<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\JobPositionUpdateActions;
use App\Admin\Repositories\JobPositionUpdate;
use App\Models\JobPositionUpdateModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use SuperEggs\DcatDistpicker\DcatDistpickerHelper;

class JobPositionUpdateController extends AdminController
{
    protected $title='职位信息修改申请';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new JobPositionUpdate(['jobPositionInfo']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('job_position_id');
            $grid->column('jobPositionInfo.position_name','职位名称');
            $grid->column('content')->display(function ($value) {
                $html = '';
                foreach ($value as $item) {
                    if (in_array($item['key'],['region_province','region_city','region_region'])){
                        $old = $item['old']>0?DcatDistpickerHelper::getAreaName($item['old']):'空';
                        $new = $item['new']>0?DcatDistpickerHelper::getAreaName($item['new']):'空';
                    }else{
                        $old = $item['old'];
                        $new = $item['new'];
                    }
                    $html .= trans("jobposition.fields.{$item['key']}") . "由 <bran style='color: red'>$old</bran> 改为 <bran class='text-success'>$new</bran></br>";
                }
                return $html;
            })->width(1000);
            $grid->column('cur_status')->width(50)->using(JobPositionUpdateModel::STATUS_LIST)
                ->label([
                    1 => 'blue',
                    2 => 'success',
                    3 => 'red',
                ]);
            $grid->column('created_at','修改时间');
            $grid->actions(function (Grid\Displayers\Actions $actions){
                if ($this->cur_status == 1){
                    $actions->append(new JobPositionUpdateActions(2));
                    $actions->append(new JobPositionUpdateActions(3));
                }

            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('cur_status')->select(JobPositionUpdateModel::STATUS_LIST);

            });
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            $grid->disableEditButton();
            //边框模式
//            $grid->withBorder();
            //横向滚动条
//            $grid->scrollbarX();
            // 禁用批量删除按钮
            $grid->disableBatchDelete();
            // 禁用显示按钮
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new JobPositionUpdate(), function (Form $form) {
            $form->display('id');
            $form->text('job_position_id');
            $form->text('content');
            $form->text('cur_status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
